package com.xtsoft.platform.base.system.domain;

/*
*    上海信拓信息技术有限公司  
*    Copyright (C) ShangHai XinTuo Software Inc.
*/

import com.fasterxml.jackson.annotation.JsonIgnore;

import com.xtsoft.core.entity.GenericEntity;
import com.xtsoft.core.utils.StringUtils;
import com.xtsoft.platform.globe.enums.YesNo;
import com.xtsoft.platform.globe.interfaces.CommonUser;
import lombok.Data;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;


import jakarta.persistence.*;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SysUser Base Java Bean, base class for the model, mapped directly to database table
 * 
 * Avoid changing this file if not necessary, will be overwritten. 
 *
 * 用户表
 */
@Entity
@Table(name = "sys_user")
@Data
//@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","sysOrganize","sysBlobs","userDesktops","sysRoles","resources"})
//@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class SysUser extends GenericEntity implements UserDetails {


	@Id
	@GeneratedValue(generator = "system-uuid")
	@GenericGenerator(name = "system-uuid", strategy = "uuid")
	@Column(name="userId",nullable=false,length=32)
    private String userId;




	/**
	*   用户名
	*/
	@Column(name="username",nullable=false, length=64 )
	private String username;


	/**
	*   登录ID
	*/
	@Column(name="loginId",nullable=false, unique=true, length=64 )
	private String loginId;


	/**
	*   密码
	*/
	@Column(name="password",nullable=false, length=128 )
	private String password;


	/**
	*   是否可视
	*/
	@Column(name="isVisable",nullable=false, length=10 )
	private Integer isVisable;


	/**
	*   移动电话
	*/
	@Column(name="mobile",nullable=true, length=32 )
	private String mobile;


	/**
	*   邮箱
	*/
	@Column(name="email",nullable=true, length=128 )
	private String email;


	/**
	*   QQ
	*/
	@Column(name="qnum",nullable=true, length=32 )
	private String qnum;


	/**
	*   状态
	*/
	@Column(name="state",nullable=false, length=10 )
	private Integer state;

	/**
	*   用户头象位置
	*/
	@Column(name="filePath",nullable=true, length=128 )
	private String filePath;
	
	/**
	*   说明
	*/
	@Column(name="descript",nullable=true, length=255 )
	private String descript;
	
    /**
    *importedKeys
    */ 
	@ManyToOne(fetch = FetchType.LAZY,optional=false)
	@JoinColumn(name="organizeId")
	private SysOrganize sysOrganize;

	
	@OneToMany(mappedBy = "sysUser", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JsonIgnore
	private List<UserRole> userRoles = new ArrayList();
	
  
    @Transient
    @JsonIgnore 
    private List<SysResource> resources = new ArrayList();
    
	@Transient
	@JsonIgnore
	private List<SysRole> sysRoles = new ArrayList();
	

	
	/**
	 * 修改个人用户时保存新密码
	 * @param oldPassword
	 */
	
	public void setOldPassword(String oldPassword){
		this.setPassword(oldPassword);
	}
	
	public void setStringPassword(String stringPassword){
		if(StringUtils.isEmpty(stringPassword)){
			setOldPassword(StringUtils.encryptSha256(password));
		}else{
			this.setPassword(StringUtils.encryptSha256(stringPassword));
		}
	}
	
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    
    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return this.getState().equals(YesNo.Yes.getIntValue());
    }
    
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> auths = new ArrayList();
		List<SysRole> roles = this.getSysRoles();
	    roles.forEach(action -> {
			auths.add(new SimpleGrantedAuthority(action.getAlias()));
		});
		return auths;
	}
	
	@Override
	public boolean equals(Object obj) {  
	    if (obj instanceof SysUser) {  
	        return this.loginId.equals(((SysUser) obj).getLoginId());  
	    }  
	    return false;  
	}
	public String toString() {
		return new ToStringBuilder(this).append("userId", this.userId).append("username", this.username)
				.append("password", this.password).append("loginId", this.loginId).append("mobile", this.mobile)
				.append("email", this.email).append("qnum", this.qnum).append("state", this.state)
				.append("isVisable", this.isVisable).append("filePath", this.filePath).append("descript", this.descript)
				.toString();
	}
	public boolean contains(String roleName) {
		List<SysRole> roles = getSysRoles();
		roles = roles.stream().filter(action->action.getAlias().equals(roleName)).collect(Collectors.toList());
		return roles.size() > 0?true:false;
	}
	
	@Override  
	public int hashCode() {  
	    return this.loginId.hashCode();  
	}


}
